/**
 * Created by yuzhou on 16/9/23.
 */

define([
    'vue','debug', 'text!tpl/goodAdd.html',
    'vuex/actions/good', 'vue-select'
], function(Vue, Debug, goodAddHtml, goodAction, VueSelect) {

    return Vue.extend({
        template: goodAddHtml,
        data: function(){
            return {
                type: null,
                brandId: null,
                goodId: null,
                result: null
            }
        },
        watch: {
            type: function(newType, oldType) {
                if(newType && !(newType in this.brands)) {
                    this.getBrands(newType)
                }
                this.brandId = null
            },
            brandId: function(newType, oldType) {
                if(newType && !(newType in this.goods)) {
                    this.getExtGoods(newType)
                }
                this.goodId = null
            }
        },
        vuex: {
            getters: {
                types: function(store) { return store.good.types },
                brands: function(store) { return store.good.brands },
                goods: function(store) { return store.good.goods }
            },
            actions: {
                getTypes: goodAction.getTypes,
                getBrands: goodAction.getBrands,
                getExtGoods: goodAction.getExtGoods,
                save: goodAction.saveGood
            }
        },
        components: {
            'v-select': VueSelect.VueSelect
        },
        methods: {
            submit: function() {
                var me = this
                if(this.goodId){
                    this.save({
                        extId: this.goodId
                    }, function(good){
                        me.result = {
                            'ok': true,
                            'title': '导入成功',
                            'msg': '商品' + good.id +'导入成功'
                        }
                    }, function(error){
                        me.result = {
                            'ok': false,
                            'title': '导入失败',
                            'msg': error
                        }
                    })
                }
            }
        },
        ready: function(){
            this.getTypes()
        }
    })
})